﻿//********************************************************************************//
//创建日期：<创建日期，2017-06-13>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：处理报表相关
//********************************************************************************//

using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using Entity;
using YXYLCommon;
using BLL;
using System.Web.SessionState;
using System.Text;
using System;
using BLL.Access;

namespace Web.ashx
{

    /// <summary>
    /// Common 的摘要说明
    /// </summary>
    public class LaiHan : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["op"] == "DJCX")//获取登记来函
            {
                string glk = context.Request["glk"];
                string lhlx = string.IsNullOrEmpty(context.Request["lhlx"]) || context.Request["lhlx"] == "0" ? null : context.Request["lhlx"];
                string lhbh = context.Request["lhbh"];
                string yhh = context.Request["yhh"];
                string xkslh = context.Request["xkslh"];
                JsonRs rs = new JsonRs();
                if (context.Session.Timeout <= 0)
                {
                    rs.Msg = "会话已过期";
                    rs.Data1 = "session";
                    context.Response.Write(JsonConvert.SerializeObject(rs));
                    return;
                }
                //string zd = "00";
                string zd = BusinessTool.WType != 0 || BusinessTool.ZHANDIANBH() == "00" ? null : BusinessTool.ZHANDIANBH();
                List<LHLaiHanXX> list = new DALLHLaiHanXX().DJCX(BusinessTool.WType, glk, null, lhbh, yhh, xkslh, lhlx, zd);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "FSLIST")//获取复审修改来函列表
            {
                string zd = context.Request["zd"];
                string glk = context.Request["glk"];
                string lhbh = context.Request["lhbh"];
                string yhh = context.Request["yhh"];
                //string y = context.Request["y"];
                //string m = context.Request["m"];
                int fs = int.Parse(context.Request["fs"]);
                List<LHLaiHanXX> list = new DALLHLaiHanXX().FUSHENXGLIST(zd, glk, lhbh, yhh, null, null, fs);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "DJSC")//删除来函记录
            {
                string lhbh = context.Request["lhbh"];
                context.Response.Write(new SqlHelper().DeleteLaihan(lhbh));
            }
            else if (context.Request["op"] == "YUETIAOZQRXXHQ")//获取复月度调整确认信息
            {
                string yhh = context.Request["yhh"];
                int userid=LoginInfo.CurrentUser.UserID;
                //int userid = 55;
                List<LHYueDuQRXX> list = new DALLHYueDuQRXX().ListGet(yhh, userid);
                var count = new LHYueDuQRXX
                {
                    S_YONGHUM = "合计",
                    I_SYL = 0,
                    I_SYJH = 0,
                    I_JJL = 0
                };
                foreach (var item in list)
                {
                    count.I_SYL += item.I_SYL;
                    count.I_SYJH += item.I_SYJH;
                    count.I_JJL += item.I_JJL;
                }
                list.Add(count);
                LayerJsonRs jrs = new LayerJsonRs(list.Count-1, list);
                context.Response.Write(JsonConvert.SerializeObject(jrs));
            }
            else if (context.Request["op"] == "YUETIAOZQR")//月度确认页面确认
            {
                string yhhs = context.Request["yhhs"];
                string jjls = context.Request["jjls"];
                int userid=LoginInfo.CurrentUser.UserID;
                //int userid = 56;
                DbCmdResult dbrs = new DALLHYueDuQRXX().ListQR(yhhs,jjls,userid);
                context.Response.Write(JsonConvert.SerializeObject(new JsonRs(dbrs.IsSuccess, dbrs.ReturnMsg, null)));
            }
            else if (context.Request["op"] == "YUETIAOZDJXX")//月度登记信息
            {
                string yhh = context.Request["yhh"];
                int userid = LoginInfo.CurrentUser.UserID;
                List<LHYueDuQRXX> list = new DALLHYueDuQRXX().DJXXList(yhh, BusinessTool.GetWaterYM(),userid);
                LayerJsonRs jrs = new LayerJsonRs(list.Count, list);
                context.Response.Write(JsonConvert.SerializeObject(jrs));
            }
            else if (context.Request["op"] == "YUETIAOZQRQX")//月度确认页面确认取消
            {
                string yhhs = context.Request["yhhs"];
                DbCmdResult dbrs = new DALLHYueDuQRXX().ListQX(yhhs);
                context.Response.Write(JsonConvert.SerializeObject(new JsonRs(dbrs.IsSuccess, dbrs.ReturnMsg, null)));
            }
            else if (context.Request["op"] == "QFXXLISTHTML")//获取历史欠费信息
            {
                string yhh = context.Request["yhh"];
                List<YSQFXX> list = new DALYSQFXX().Get(yhh);
                StringBuilder sb = new StringBuilder();
                string tds = " style='width:16.6%;text-align:right'";
                sb.Append("<table class='layui-table' style='width:98%;margin:auto'><thead><th style='text-align:center'>水量年月</th><th style='text-align:centerr'>实用量</th><th style='text-align:center'>加价量</th><th style='text-align:center'>应收金额</th><th style='text-align:center'>实收金额</th><th style='text-align:center'>欠费金额</th></thead><tbody>");

                for (int i = 0; i < list.Count; i++)
                {
                    sb.Append("<tr><td style='text-align:center'>" + list[i].I_YM + "</td><td" + tds + ">" + list[i].I_SYL + "</td><td" + tds + ">" + list[i].I_JJL + "</td><td" + tds + ">" + list[i].N_YSJE.ToString("0.00") + "</td><td" + tds + ">" + list[i].N_SSJE.ToString("0.00") + "</td><td" + tds + ">" + list[i].N_QFJE.ToString("0.00") + "</td></tr>");
                }
                sb.Append("</tbody><table>");
                context.Response.Write(sb.ToString());
            }
            else if (context.Request["op"] == "ISWork")//是否工作日
            {
                int? err;
                int year = Convert.ToInt32(context.Request["year"]);
                int month = Convert.ToInt32(context.Request["month"]);
                int day = Convert.ToInt32(context.Request["day"]);
                SpHelper sh = new SpHelper();
                sh.P_DATE_RETURNLAIHANROWCOLOR(year, month, day, out err);
                 context.Response.Write(err);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}